﻿using System;
using System.Collections.Generic;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Media.Animation;
using monitor_2_DBwork;

namespace monitor_server_2_WPF.DeviceListParamPanels
{
    /// <summary>
    /// Логика взаимодействия для AddDevicePanel.xaml
    /// </summary>
    public partial class AddStationPanel
    {

        //private static List<string> _blrList; 
        private static string _region;
        private static readonly DeviceListTabControl.UpdateRegionTreeNodeDelegate UpdateRegionTreeNode = DeviceListTabControl.UpdateRegion;

        private AddStationPanel()
        {
            InitializeComponent();
            BlrLabel.Content = Res.Rm.GetString("BLR");
            //BlrBox.ItemsSource = _blrList;
            StationLabel.Content = Res.Rm.GetString("Station");
            AcceptButton.Content = new MyDockPanel(Res.GetBitMapFromPng("accept_image_16"), Res.Rm.GetString("Add"));
            EmptyBlrInfoBox.Text = "Select frequency zone.";
            EmptyStationInfoBox.Text = "Input station.";
            ExistStationInfoBox.Text = "Station already exist";
            _infos.Add(InfoType.EmptyBlr);
            _infos.Add(InfoType.EmptyStation);
        }

        //private static AddDevicePanel _panel;

        public static UIElement GetPanel(string region)
        {
            var panel = new AddStationPanel();
            var blrList = DBwork.GetBlrList();
            panel.BlrBox.ItemsSource = blrList;
            _region = region;
            //panel.MainGrid.Visibility = Visibility.Visible;
            return panel;
        }

        private void AcceptButton_Click(object sender, RoutedEventArgs e)
        {
            if (BlrBox.SelectedItem == null)
            {
                AddInfo(InfoType.EmptyBlr);
            }
            if (StationBox.Text.Trim() == "")
            {
                AddInfo(InfoType.EmptyStation);
            }
            if (_infos.Count > 0)
                return;
            if (!DBwork.AddStation(StationBox.Text, _region, BlrBox.Text, ""))
            {
                MessageBox.Show(Res.Rm.GetString("Error"));
            }
            else
            {
                MessageBox.Show(Res.Rm.GetString("Complete"));
                UpdateRegionTreeNode();
            }
        }

        private void AddInfo(InfoType type)
        {
            if(AcceptButton.IsEnabled)
                AcceptButton.IsEnabled = false;
            if (_infos.Contains(type)) return;
            _infos.Add(type);
            switch (type)
            {
                case InfoType.EmptyBlr:
                    ShowInfoBox(EmptyBlrInfoBox);
                    break;
                case InfoType.EmptyStation:
                    ShowInfoBox(EmptyStationInfoBox);
                    break;
                case InfoType.ExistStation:
                    ShowInfoBox(ExistStationInfoBox);
                    break;
            }
        }

        private void RemoveInfo(InfoType type)
        {
            
            if (!_infos.Contains(type)) return;
            _infos.Remove(type);
            if (_infos.Count < 1)
                AcceptButton.IsEnabled = true;
            switch (type)
            {
                case InfoType.EmptyBlr:
                    HideInfoBox(EmptyBlrInfoBox);
                    break;
                case InfoType.EmptyStation:
                    HideInfoBox(EmptyStationInfoBox);
                    break;
                case InfoType.ExistStation:
                    HideInfoBox(ExistStationInfoBox);
                    break;
            }
        }

        private void ShowInfoBox(TextBlock tb)
        {
            tb.Visibility = Visibility.Visible;
            var a = new DoubleAnimation(0.0, 1.0, new Duration(new TimeSpan(0, 0, 0, 1)));
            tb.BeginAnimation(OpacityProperty, a);
        }

        private void HideInfoBox(TextBlock tb)
        {
            var a = new DoubleAnimation(1.0, 0.0, new Duration(new TimeSpan(0, 0, 0, 0, 500)));
            tb.BeginAnimation(OpacityProperty, a);
            tb.Visibility = Visibility.Collapsed;
            if (ContentPanel.VerticalAlignment == VerticalAlignment.Bottom)
                ContentPanel.VerticalAlignment = VerticalAlignment.Top;
        }

        private readonly List<InfoType> _infos = new List<InfoType>(3); 

        enum InfoType
        {
            EmptyBlr,
            EmptyStation,
            ExistStation
        }

        private void BlrBox_OnSelectionChanged(object sender, SelectionChangedEventArgs e)
        {
            RemoveInfo(InfoType.EmptyBlr);
        }

        private void StationBox_OnTextChanged(object sender, TextChangedEventArgs e)
        {
            var box = sender as TextBox;
            if (box.Text.Trim() == "")
            {
                AddInfo(InfoType.EmptyStation);
                RemoveInfo(InfoType.ExistStation);
                return;
            }
            RemoveInfo(InfoType.EmptyStation);
            var chk = DBwork.CheckStationExistance(box.Text.Trim());
            if (chk.StartsWith("err:"))
            {
                MessageBox.Show(chk.Remove(0, 4));
                return;
            }
            if (chk == "1")
            {
                AddInfo(InfoType.ExistStation);
            }
            else
            {
                RemoveInfo(InfoType.ExistStation);
            }
        }
    }
}
